// SPDX-License-Identifier: GPL-2.0
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>

#define DEVICE_NAME "chdev01"
#define BUFFER_SIZE 1024

static int major_number;
static struct class* device_class = NULL;
static struct cdev device_cdev;

static char message[BUFFER_SIZE] = {0};
static short message_length;

static int device_open(struct inode *inode, struct file *file) {
    printk(KERN_INFO "chdev: Device opened\n");
    return 0;
}

static int device_release(struct inode *inode, struct file *file) {
    printk(KERN_INFO "chdev: Device closed\n");
    return 0;
}

static ssize_t device_read(struct file *file, char __user *buffer, size_t len, loff_t *offset) {
    int error_count = 0;

    if (*offset >= message_length) {
        return 0; // EOF
    }

    if (*offset + len > message_length) {
        len = message_length - *offset;
    }

    error_count = copy_to_user(buffer, message + *offset, len);
    if (error_count == 0) {
        *offset += len;
        printk(KERN_INFO "SimpleCharDevice: Sent %zu characters to the user\n", len);
        return len;
    } else {
        printk(KERN_ALERT "SimpleCharDevice: Failed to send %d characters to the user\n", error_count);
        return -EFAULT;
    }
}

static ssize_t device_write(struct file *file, const char __user *buffer, size_t len, loff_t *offset) {
    if (len > BUFFER_SIZE - 1) {
        len = BUFFER_SIZE - 1; // Prevent overflow
    }

    if (copy_from_user(message, buffer, len)) {
        return -EFAULT;
    }

    message[len] = '\0'; // Null-terminate the string
    message_length = len;
    printk(KERN_INFO "SimpleCharDevice: Received %zu characters from the user\n", len);
    return len;
}

static struct file_operations fops = {
    .open = device_open,
    .release = device_release,
    .read = device_read,
    .write = device_write,
};

static int __init simple_char_device_init(void) {
    // 动态分配一个主设备号
    major_number = register_chrdev(0, DEVICE_NAME, &fops);
    if (major_number < 0) {
        printk(KERN_ALERT "chdev: Failed to register a major number\n");
        return major_number;
    }

    // 创建一个设备类
    device_class = class_create(DEVICE_NAME);
    if (IS_ERR(device_class)) {
        unregister_chrdev(major_number, DEVICE_NAME);
        printk(KERN_ALERT "chdev: Failed to create device class\n");
        return PTR_ERR(device_class);
    }

    // 初始化cdev结构
    cdev_init(&device_cdev, &fops);
    if (cdev_add(&device_cdev, MKDEV(major_number, 0), 1) == -1) {
        class_destroy(device_class);
        unregister_chrdev(major_number, DEVICE_NAME);
        printk(KERN_ALERT "chdev: Failed to add cdev\n");
        return -1;
    }

    // 创建设备文件
    device_create(device_class, NULL, MKDEV(major_number, 0), NULL, DEVICE_NAME);
    printk(KERN_INFO "chdev: Device class and device created\n");
    return 0;
}

static void __exit simple_char_device_exit(void) {
    device_destroy(device_class, MKDEV(major_number, 0));
    class_unregister(device_class);
    class_destroy(device_class);
    cdev_del(&device_cdev);
    unregister_chrdev(major_number, DEVICE_NAME);
    printk(KERN_INFO "chdev: Device removed\n");
}

module_init(simple_char_device_init);
module_exit(simple_char_device_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jason Hu");
MODULE_DESCRIPTION("A simple character device driver");
MODULE_VERSION("1.0");